package io.gitee.zhangbinhub.admin.oauth.controller.openinner

import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.admin.common.api.CommonPath
import io.gitee.zhangbinhub.admin.common.api.OauthApi
import io.gitee.zhangbinhub.admin.common.base.BaseController
import io.gitee.zhangbinhub.admin.oauth.entity.Organization
import io.gitee.zhangbinhub.admin.oauth.mapper.OrganizationMapper
import io.gitee.zhangbinhub.admin.oauth.service.OrganizationService
import io.gitee.zhangbinhub.admin.resource.server.vo.OrganizationVo
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.noear.solon.annotation.Controller
import org.noear.solon.annotation.Mapping
import org.noear.solon.annotation.Path
import org.noear.solon.core.handle.MethodType
import org.noear.solon.core.util.MimeType
import org.noear.solon.validation.annotation.NotBlank
import org.noear.solon.validation.annotation.Valid

@Valid
@Controller
@Api("机构信息（内部开放接口）")
open class OpenInnerOrgController(private val organizationService: OrganizationService) : BaseController() {
    private fun listToVo(organizationList: List<Organization>): List<OrganizationVo> =
        organizationList.map { organization -> OrganizationMapper.INSTANCE.entityToVo(organization) }

    @ApiOperation("获取机构列表", notes = "查询所有机构列表")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.openInnerBasePath + OauthApi.orgConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    fun orgList(): List<OrganizationVo> = listToVo(organizationService.getAllOrgList())

    @ApiOperation("获取机构详细信息")
    @Mapping(
        method = [MethodType.GET],
        value = CommonPath.openInnerBasePath + OauthApi.orgConfig + "/{orgId}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun orgInfo(
        @ApiParam("机构id", required = true)
        @NotBlank(message = "机构id不能为空")
        @Path(name = "orgId")
        orgId: String
    ): OrganizationVo = organizationService.getOrgInfo(orgId)
}